package com.ck.express.utils

import android.content.Context
import android.util.Log
import java.net.Inet4Address
import java.net.NetworkInterface
import java.util.Collections

// 设备信息工具类 - DeviceUtils.kt
object DeviceUtils {
    // 获取设备MAC地址
    fun getMacAddress(context: Context): String {
        try {
            val interfaces = NetworkInterface.getNetworkInterfaces()
            while (interfaces.hasMoreElements()) {
                val networkInterface = interfaces.nextElement()
                val macBytes = networkInterface.hardwareAddress ?: continue
                val sb = StringBuilder()
                for (b in macBytes) {
                    sb.append(String.format("%02X:", b))
                }
                if (sb.isNotEmpty()) sb.deleteCharAt(sb.length - 1)
                return sb.toString()
            }
        } catch (e: Exception) {
            Log.e("DeviceUtils", "获取MAC地址失败", e)
        }
        return "00:00:00:00:00:00" // 默认值
    }

    // 获取设备IP地址
    fun getLocalIpAddress(): String {
        try {
            val interfaces = Collections.list(NetworkInterface.getNetworkInterfaces())
            for (intf in interfaces) {
                val addrs = Collections.list(intf.inetAddresses)
                for (addr in addrs) {
                    if (!addr.isLoopbackAddress && addr is Inet4Address) {
                        return addr.hostAddress ?: ""
                    }
                }
            }
        } catch (e: Exception) {
            Log.e("DeviceUtils", "获取IP地址失败", e)
        }
        return "127.0.0.1" // 默认值
    }
}